# -*- coding: utf-8 -*-
# @Time : 2022-01-15 17:46
# @Author : Z
# @Function :

import requests
from jsonpath import jsonpath


def get_data(params: str):
    if params:
        param_dict = {}
        # 原始数据 app=weather.today&weaid=1&appkey=10003
        # 通过 & 分割数据，分割后的数据为列表：[app=weather.today', 'weaid=1', 'appkey=10003']
        params = params.split('&')
        print(params)
        # 成员遍历列表，通过键和值，组成字典
        for key_value in params:
            # 通过ke_value.find('=')，返回索引值，
            # [:索引值]，切片到索引值-1的位置
            key = key_value[:key_value.find('=')]
            # print(key)
            # [索引值:]，切片到  索引值~结束的位置
            value = key_value[key_value.find('=') + 1:]
            # print(value)
            param_dict[key] = value
        return param_dict
    else:
        return None


a = 'a=1&b=2&c=3'
di = get_data(a)
print(di)

# params=get_data('app=weather.today&weaid=1&appkey=10003')
#
# a = "0123456"
# b = a[:1]
# print(b)
# # 快速倒序
# c = a[::-1]
# print(c)
# # 截取后三位，-1表示最后一位
# d = a[-1:-4:-1]
# print(d)

# s = "hello daf"
# ll = list(reversed(s))
# print(ll)
# # 语法： 'sep'.join(seq)
# # 参数说明
# # sep：分隔符。可以为空
# # seq：要连接的元素序列、字符串、元组、字典
# # 上面的语法即：以sep作为分隔符，将seq所有的元素合并成一个新的字符串
# # 返回值：返回一个以分隔符sep连接各个元素后生成的字符串
# reversed_s = "".join(ll)
# print(reversed_s)

# session = requests.session()
# r = session.get('http://api.k780.com/?', params=get_data(
#     'app=weather.today&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json'))
# print(r.text)
# print(type(r.json()))
# cityname = jsonpath(r.json(), '$.result.citynm')[0]
# print(cityname)
# app = jsonpath(r.json(), 'success')
# print(app[0])

# ll = {"success": "1", "result": {"weaid": "1", "days": "2022-06-10", "week": "星期五", "cityno": "beijing", "citynm": "北京",
#                                  "cityid": "101010100", "temperature": "28℃/19℃", "temperature_curr": "27℃",
#                                  "humidity": "54%", "aqi": "69", "weather": "小雨", "weather_curr": "多云",
#                                  "weather_icon": "http://api.k780.com/upload/weather/d/1.gif", "weather_icon1": "",
#                                  "wind": "东风", "winp": "2级", "temp_high": "28", "temp_low": "19", "temp_curr": "27",
#                                  "humi_high": "0", "humi_low": "0", "weatid": "2", "weatid1": "", "windid": "2",
#                                  "winpid": "2", "weather_iconid": "1"}}
# print(type(ll))

# cityname = jsonpath(ll, '$.result.citynm')[0]
# print(cityname)
